package com.eqs.enterprisequestionnaire.controller;

import com.eqs.enterprisequestionnaire.model.pojo.Option;
import com.eqs.enterprisequestionnaire.model.pojo.Question;
import com.eqs.enterprisequestionnaire.result.R;
import com.eqs.enterprisequestionnaire.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @PostMapping("/create")
    public R<Question> createQuestion(@RequestBody Question question) {
        questionService.insertQuestion(question.getQuestionnaireId(), question.getType(), question.getContent());
        return R.success(questionService.getQuestionAfterCreat(question));
    }
    @GetMapping("/get/{id}")
    public R<Question> getQuestionById(@PathVariable Integer id) {
        Question question = questionService.selectByQuestionId(id);
        if (question != null) {
            return R.success(question);
        } else {
            return R.error("Not Found Question");
        }
    }
    @PutMapping("/update")
    public R<String> updateQuestion(@RequestBody Question question) {
        try {
            questionService.updateQuestion(question);
            return R.success("Question updated successfully");
        } catch (RuntimeException e) {
            return R.error("Update failed");
        }
    }
    @DeleteMapping("/delete/{id}")
    public R<String> deleteQuestion(@PathVariable Integer id) {
        questionService.deleteQuestion(id);
        return R.success("Question deleted successfully");
    }
    @PostMapping("/displayOptions")
    public R<List<Option>> displayOption(@RequestBody Question question) {
        return R.success(questionService.displayOption(question));
    }
}

